/* eslint-disable import/no-extraneous-dependencies */

import gulp from 'gulp';
import babel from 'gulp-babel';
import del from 'del';
import eslint from 'gulp-eslint';
import webpack from 'webpack-stream';
import webpackConfig from './webpack.config.babel';

const paths = {
  src: 'src/**/*.js',
  dist: 'build/',
  gulpFile: 'gulpfile.babel.js',
  webpackFile: 'webpack.config.babel.js',
  clientBundle: 'build/bundle.js?(map)',
  clientEntryPoint: 'src/index.js'
};

gulp.task('lint', () => {
  gulp.src([paths.src, paths.gulpFile, paths.webpackFile])
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(eslint.failAfterError());
});

gulp.task('clean', () => del(paths.dist));

gulp.task('build', ['lint', 'clean'], () => {
  gulp.src(paths.src)
    .pipe(babel())
    .pipe(gulp.dest(paths.dist));
});

gulp.task('main', ['build'], () => {
  gulp.src(paths.clientEntryPoint)
    .pipe(webpack(webpackConfig))
    .pipe(gulp.dest(paths.dist));
});

gulp.task('watch', () => {
  gulp.watch(paths.src, ['build']);
});

gulp.task('default', ['watch', 'main']);
